package com.sphd.Restaurant.presenter;

import android.widget.Toast;

import com.android.volley.VolleyError;
import com.sphd.Restaurant.infos.AddressInfo;
import com.sphd.Restaurant.interfFace.VolleyCallBack;
import com.sphd.Restaurant.utils.JsonUtil;
import com.sphd.Restaurant.utils.VolleyErrorHelper;
import com.sphd.Restaurant.views.AddressesView;


import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by Hu on 2016/10/12.
 */

public class AddressesP extends BasePresenter implements VolleyCallBack<JSONObject> {
    AddressesView addressesView;

    public AddressesP(AddressesView addressesView) {
        this.addressesView = addressesView;
    }

    @Override
    public void addData(String url) {
        addressesView.showdialog();
        sendJsonObjectUrl(url, this);
    }

    @Override
    public void onResponse(JSONObject object) {
        try {
            List<AddressInfo> addressInfoList = JsonUtil.getListFromString(object.getString("res"), AddressInfo.class);
           addressesView.sendAddressList(addressInfoList);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        addressesView.dissmissdialog();
    }

    @Override
    public void onErrorResponse(VolleyError error) {
        addressesView.dissmissdialog();
        Toast.makeText(context, VolleyErrorHelper.getMessage(error),Toast.LENGTH_SHORT).show();
    }
}
